
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

public class OptionScreen extends GameCanvas implements Runnable, CommandListener {

    private int delay = 100;
    private Image impNotSound;
    private Image impIsSound;
    private Image backGround;
    private Graphics g;
    private boolean isActive;
    private Main iMain;
    private MenuScreen menuScreen;
    private Command cmdBack;
    private static int chon = 1;
    private Player clickSound;

    public OptionScreen(Main aMain, MenuScreen aMenu) {
        super(true);
        setFullScreenMode(true);
        iMain = aMain;
        menuScreen = aMenu;

        cmdBack = new Command("OK", Command.EXIT, 0);
        this.addCommand(cmdBack);
        setCommandListener(this);
    }

    public void Start() {
        try {
            impNotSound = Image.createImage("optionNotSound.png");
            impIsSound = Image.createImage("optionIsSound.png");
        } catch (Exception e) {
        };

        try {
            //moveSound = Manager.c
            InputStream in2 = getClass().getResourceAsStream("/click.wav");
            clickSound = Manager.createPlayer(in2, "audio/x-wav");
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }

        isActive = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        if(chon == 0)
            Draw(impNotSound);
        else 
            Draw(impIsSound);
        while (isActive) {
            processKeys(getKeyStates());
            flushGraphics();
            try {
                Thread.sleep(delay);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void processKeys(int keyCode) {
        switch (keyCode) {
            case LEFT_PRESSED:
                if (chon == 1) {
                    Draw(impNotSound);
                    chon = 0;
                }
                break;
            case RIGHT_PRESSED:
                if (chon == 0) {
                    Draw(impIsSound);
                    chon = 1;
                }
                try {
                    clickSound.start();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
                break;
            case DOWN_PRESSED:
                if (chon == 1) {
                    Draw(impNotSound);
                    chon = 0;
                }

                break;
            case UP_PRESSED:
                if (chon == 0) {
                    Draw(impIsSound);
                    chon = 1;
                }
                try {
                    clickSound.start();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
                break;
        }
    }

    public void Draw(Image img) {
        this.g = this.getGraphics();

        g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP);
        this.flushGraphics();
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command == cmdBack) {
            if (chon == 1) {
                try {
                    clickSound.start();
                } catch (MediaException ex) {
                    ex.printStackTrace();
                }
            }

            Display.getDisplay(iMain).setCurrent(menuScreen);
            stop();
        }
    }

    public static int getChon() {
        return chon;
    }

    public void stop() {
        isActive = false;
    }
}
